<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 16/9/1
 * Time: 下午6:37
 */
class Base extends MY_Admin_Controller
{
    public function index()
    {
        $this->load->model('recommend/Model_recommend','recommend');
        $recommendInfo = $this->recommend->getAllLists();

        $data = array
        (
            'recommendInfo' => $recommendInfo,
        );

        $this->load->view('admin/recommend/base/index',$data);
    }


    public function create()
    {
        $rid = (int)$this->uri->segment(4);

        //竞拍等级
        $this->load->model('recommend/Model_recommend','recommend');
        $recommendInfo = $this->recommend->getById($rid);

        $data = array(
            'recommendInfo'       =>    $recommendInfo,
        );

        $this->load->view('admin/recommend/base/create',$data);
    }

    /*
     * 竞拍资料编辑后保存
     *
     **/
    public function save()
    {

        $rid = (int)$this->input->get_post('rid');

        $data = array(
            'name'           =>     trim($this->input->get_post('name')),
            'identification' =>     trim($this->input->get_post('identification')),
            'support_type'   =>     (int)$this->input->get_post('support_type'),
            'width'          =>     (int)$this->input->get_post('width'),
            'height'         =>     (int)$this->input->get_post('height'),
            'view_num'       =>     $this->input->get_post('view_num'),
            'descr'          =>     trim($this->input->get_post('descr')),
            'status'         =>     (int)$this->input->get_post('status'),
            'template'       =>     $this->input->get_post('template'),
            'open_status'    =>     (int)$this->input->get_post('open_status'),
        );

        //更新
        $this->load->model('recommend/Model_recommend','recommend');
        $this->recommend->save($data, $rid ? $rid : null);

        $this->load->helper('url');
        redirect(url('admin').'recommend/base/index');
    }

    /*
     * 推荐位内容
     * */
    public function content()
    {
        $rid = $this->uri->segment(4);

        $this->load->model('recommend/Model_recommend_content','content');
        $contentInfo = $this->content->lists(0,100,array('rid'=>$rid));

        $this->load->model('recommend/Model_recommend','recommend');
        $title = $this->recommend->getById($rid);

        $data = array(
            'contentInfo' => array_values($contentInfo),
            'title'       => $title['name'],
            'rid'         => $rid
        );

        $this->load->view('admin/recommend/base/content',$data);
    }

    /*
     * 推荐详细展示
     * */
    public function content_create()
    {
        //获取推荐内容的id
        $cid = (int)$this->uri->segment(5);
        $rid = (int)$this->uri->segment(4);

        $this->load->model('recommend/Model_recommend_content','content');
        $contentInfo = $this->content->getById($cid);

        $this->load->model('user/Model_user','user');
        $list = $this->user->lists('0','100000',array(),true,false,'uid,uname');
        //uid为key uname为val
        $userInfo = array();
        foreach($list as $v){
            $userInfo[$v['uid']] = $v['uname'];
        }

        $this->load->model('recommend/Model_recommend','recommend');
        $recommend = $this->recommend->getAllLists();
        $recommendInfo = array();
        foreach($recommend as $v){
            $recommendInfo[$v['rid']] = $v['name'];
        }


        $data = array(
            'contentInfo'       =>    $contentInfo,
            'user'              =>    $userInfo,
            'recommendInfo'     =>    $recommendInfo,
            'rid'               =>    $rid,
        );

        $this->load->view('admin/recommend/base/content_create',$data);
    }

    public function content_save()
    {
        $cid = (int)$this->input->get_post('cid');
        $rid = (int)$this->input->get_post('rid');
        $uid = (int)$this->input->get_post('uid');

        $this->load->model('user/Model_user','user');
        $userInfo = $this->user->getById($uid);
        
        $content = !empty($_FILES['pic']['tmp_name'])?$this->uploadFile('pic'):trim($this->input->get_post('content'));

        $data = array(
            'name'           =>     trim($this->input->get_post('name')),
            'rid'            =>     (int)$this->input->get_post('rid'),
            'content'        =>     $content,
            'uid'            =>     (int)$this->input->get_post('uid'),
            'uname'          =>     $userInfo['uname'],
            'link'           =>     $this->input->get_post('link'),
            'descr'          =>     $this->input->get_post('descr'),
            'sort'           =>     $this->input->get_post('sort'),
            'status'         =>     $this->input->get_post('status'),
            'start_time'     =>     trim($this->input->get_post('start_time')),
            'end_time'       =>     $this->input->get_post('end_time'),
            'type'         =>     $this->input->get_post('type'),
            'final_amount'   =>     $this->input->get_post('final_amount'),
            'pay_status'     =>     (int)$this->input->get_post('pay_status'),
            'pay_time'       =>     $this->input->get_post('pay_time'),
            'order_sn'       =>     $this->input->get_post('order_sn'),
            'callback_url'   =>     $this->input->get_post('callback_url'),
            'pay_status'     =>     (int)$this->input->get_post('pay_status'),
        );

        //更新
        $this->load->model('recommend/Model_recommend_content','content');
        $this->content->save($data, $cid ? $cid : null);

        $this->load->helper('url');
        redirect(url('admin').'recommend/base/content/'.$rid);
    }

    /**
     * 删除推荐内容
     */
    public function content_delete()
    {
        $response = array('code' => '0', 'msg' => '操作成功');

        do {
            $cid = (int)$this->input->get_post('cid');

            if (!$cid)
            {
                $response = error(70001);
                break;
            }

            $this->load->model('recommend/Model_recommend_content','content');

            $res = $this->content->delete(array('cid' => $cid));

            if (!$res)
            {
                $response = error(10080);
                break;
            }
        }while (false);


        $this->json_output($response);
    }

    /**
     * 删除推荐
     */
    public function delete()
    {
        $response = array('code' => '0', 'msg' => '操作成功');

        do {
            $rid = (int)$this->input->get_post('rid');

            if (!$rid)
            {
                $response = error(70000);
                break;
            }

            $this->load->model('recommend/Model_recommend_content','content');
            $item = $this->content->getOne(array('rid' => $rid));
            if (!empty ($item))
            {
                $response = error(70003);
                break;
            }

            $this->load->model('recommend/Model_recommend','recommend');
            $res = $this->recommend->delete(array('rid' => $rid));

            if (!$res)
            {
                $response = error(10080);
                break;
            }
        }while (false);


        $this->json_output($response);
    }
}